package core.genetic.selection;

import java.util.Arrays;

import utility.Pair;
import core.Mode;
import core.genetic.Individual;
import core.genetic.Population;

public class MinimaxSelection implements Selection{	//radi samo za populaciju deljivu sa 4

	public void select(Population pop, double[] values) {
		int length = values.length;
		int indlen = pop.get(0).getLength();
		Pair<Individual, Double>[] par = (Pair<Individual, Double>[])new Pair<?,?>[values.length];
		for (int i=0; i<values.length; i++){
			par[i] = new Pair<Individual, Double>(pop.get(i), values[i]);
		}
		Arrays.sort(par, new Pair.SecondDescendingComparator<Individual, Double>());
		
		for (int i=0; i<length/4; i+=1){
			Individual ind = new Individual(indlen);
			ind = Mode.mode.crossover.crossover(par[i].first(), par[length/2-1-i].first());
			Mode.mode.mutation.mutate(ind);
			par[length/2+2*i].first(ind);
			ind = Mode.mode.crossover.crossover(par[length/2-1-i].first(), par[i].first());
			Mode.mode.mutation.mutate(ind);
			par[length/2+2*i+1].first(ind);
		}
		
		pop.setFirst(par, length);
		
//		for (int i=length/2-1; i>=Mode.mode.elitismRate*length/2; i--){
//			Mode.mode.mutation.mutate(pop.get(i));
//		}
		
		for(int i = (int)(0.5+Mode.mode.elitismRate*pop.size());i<pop.size();i++) 			
			Mode.mode.mutation.mutate(pop.get(i));

	}

}
